import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import numeral from 'numeral';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import dataImportCreate from '../../addons/DataImport'
import * as com from '../../../utils/common';
import CompClieQylxWhForm from './CompClieQylxWhForm'
/**对公客户 */
const daoName = "CM_CORP_CUST_DIM";
const ButtonGroup = Button.Group;        
const renderNumber = (num) => {       
  let value = ''
  if (num) {
    value = numeral(num).format('0,0')
  }
  return value
};
const columns = [
  { title: '组织机构代码', width: 140, sorter: true, dataIndex: 'Zzjgdm', key: 'Zzjgdm', dispaly: true, filter: true,export:true },
  { title: '客户名称', width: 250, sorter: true, nowrap: true, caption: "KHMC", key: 'KHMC', dispaly: true, filter: true ,export:true },
  { title: '最早开户日期', width: 140, sorter: true, key: 'Zzkhrq', caption: "Zzkhrq", dispaly: true, filter: true, render: data => datetime.format(data.Zzkhrq, "yyyy-MM-dd") },
  { title: '授信金额', width: 140, sorter: true, dataIndex: 'SXJE', key: 'SXJE', dispaly: true, filter: true ,export:true },
  { title: '是否开通网银', width: 140, sorter: true, dataIndex: 'Sfktwy', key: 'Sfktwy', dispaly: true, filter: true },
  { title: '开通网银版本种类', width: 180, sorter: true, dataIndex: 'Ktwybbzl', key: 'Ktwybbzl', dispaly: true, filter: true },
  { title: '营业执照', width: 180, sorter: true, nowrap: true, caption: "Yyzz", key: 'Yyzz', dispaly: true, filter: true },
  { title: '税务证号', width: 185, sorter: true, nowrap: true, caption: "Swzh", key: 'Swzh', dispaly: true, filter: true },
  //{ title: '企业规模行标', width: 140, sorter: true, dataIndex: 'Qygmhb', key: 'Qygmhb', dispaly: true, filter: true },
  { title: '企业规模(四部委)', width: 200, sorter: true, dataIndex: 'Qygmgj', key: 'Qygmgj', dispaly: true, filter: true },
  { title: '新行业种类代码', width: 160, sorter: true, dataIndex: 'Xhyzldm', key: 'Xhyzldm', dispaly: true, filter: true },
  { title: '是否结算户', width: 140, sorter: true, dataIndex: 'Sfjsh', key: 'Sfjsh', dispaly: true, filter: true },
  { title: '客户经理1', width: 100, sorter: false, dataIndex: 'YG1', key: 'YG1', dispaly: true, filter: false },
  { title: '分配比例1(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB1', render: data => renderNumber(data.ZB1), key: 'ZB1', dispaly: true, filter: false },
  { title: '客户经理2', width: 100, sorter: false, dataIndex: 'YG2', key: 'YG2', dispaly: true, filter: false },
  { title: '分配比例2(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB2', render: data => renderNumber(data.ZB2), key: 'ZB2', dispaly: true, filter: false },
  { title: '客户经理3', width: 100, sorter: false, dataIndex: 'YG3', key: 'YG3', dispaly: true, filter: false },
  { title: '分配比例3(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB3', render: data => renderNumber(data.ZB3), key: 'ZB3', dispaly: true, filter: false },
  { title: '客户经理4', width: 100, sorter: false, dataIndex: 'YG4', key: 'YG4', dispaly: true, filter: false },
  { title: '分配比例4(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB4', render: data => renderNumber(data.ZB4), key: 'ZB4', dispaly: true, filter: false },
  { title: '客户经理5', width: 100, sorter: false, dataIndex: 'YG5', key: 'YG5', dispaly: true, filter: false },
  { title: '分配比例5(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB5', render: data => renderNumber(data.ZB5), key: 'ZB5', dispaly: true, filter: false },
];
const controls = [
  { name: 'Zzjgdm', required: true, type: 'text', label: '组织机构代码', grid: 12 / 24 },
  { name: 'KHMC', required: true, type: 'text', label: '客户名称', grid: 12 / 24 },
  { name: 'Zzkhrq', required: true, type: 'date', label: '最早开户日期', grid: 12 / 24 },
  { name: 'SXJE', required: true, type: 'number',dataType:'number', label: '授信金额', grid: 12 / 24 },
  { name: 'Sfktwy', required: true, type: 'text', label: '是否开通网银', grid: 12 / 24 },
  { name: 'Ktwybbzl', required: true, type: 'text', label: '开通网银版本种类', grid: 12 / 24 },
  { name: 'Yyzz', required: true, type: 'text', label: '营业执照', grid: 12 / 24 },
  { name: 'Swzh', required: true, type: 'text', label: '税务证号', grid: 12 / 24 },
  //{ name: 'Qygmhb', required: true, type: 'text', label: '企业规模行标', grid: 12 / 24 },
  { name: 'Qygmgj', required: true, type: 'text', label: '企业规模四部委', grid: 12 / 24 },
  { name: 'Xhyzldm', required: true, type: 'text', label: '新行业种类代码', grid: 12 / 24 },
  { name: 'Sfjsh', required: true, type: 'text', label: '是否结算户', grid: 12 / 24 },

];
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
class CM_CORP_CUST_DIM extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataChange: false
    };
  }
  componentDidMount() {
    this.props.dispatch({
      type: `${daoName}/setKeyName`,
      payload: { keyName: "ID", permID: this.props.permID }

    });
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, page: 1, pageSize: 20 }
    });

    this.loadOptionData();
  }
  loadOptionData() {
    let options = []
    controls.map(c => {
      if (c.fetch) {
        let f = c.fetch;
        if (c.type === 'cascader-select') {
          f.isCascader = true;
        }
        if (c.type !== 'search') {
          options.push(f);
        }
      }
    })
    if (daoName && options.length > 0) {
      this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
    }
  }  
  showModelHandler(isAdd) {
    let key = null
    if (!isAdd) {
        const len = this.props.selectedRowKeys.length;
        if (len === 0) {
            message.info('请选择要维护企业类型的数据行！');
            return;
        }
        key = this.props.selectedRowKeys[len - 1]
    }
    this.props.dispatch({
        type: `${daoName}/openEdit`,
        payload: { key }
    });
  }
  findData() {
    const form = this.refs.findForm.getForm();
    let lx = '';
    let seeks = [];
    let whereItems = [];
    let WhereCauses = [];
    form.validateFields((err, values) => {
      if (!err) {
        lx = values.qygm;
      }
    });
    if (lx == 0) {
      seeks = [];
    } else {
      if (lx == 1) {
        whereItems.push({
          FieldName: "ISDKH",
          Operator: "=",
          FieldValues: '1'
        })
        seeks.push({
          WhereItems: whereItems
        })
      } else {
        if (lx == 2) {
          whereItems.push({
            FieldName: "ISYXH",
            Operator: "=",
            FieldValues: '1'
          })
          seeks.push({
            WhereItems: whereItems
          })
        } else {
          if (lx == 3) {
            whereItems.push({
              FieldName: "ISJBH",
              Operator: "=",
              FieldValues: '1'
            })
            seeks.push({
              WhereItems: whereItems
            })
          } else {
            if (lx == 4) {
              whereItems.push({
                FieldName: "ISJGH",
                Operator: "=",
                FieldValues: '1'
              });
              seeks.push({
                WhereItems: whereItems
              })
            }
          }
        }
      }
    }
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, seeks }
    });
  }
  render() {
    let { showEdit, record, permID,popedomTypes } = this.props;
    let pTypes = popedomTypes[permID]
    let data = [{ value: '1', label: '查询', onClick: this.findData.bind(this) }]
    if (pTypes.indexOf("2") !== -1) {
      data.push({ value: '2', label: '企业类型维护', onClick: this.showModelHandler.bind(this, false) })
    }
    const controlsFilter = [
      {
        name: "qygm", label: "客户类型", type: "select", grid: 4 / 24, required: true,
        dataType: "number", data: [{ value: '0', label: '全部' }, { value: '1', label: '大客户' },
        { value: '2', label: '有效户' }, { value: '3', label: '结算户' }, { value: '4', label: '机构户' }],
        layout: { labelSpan: 12, wrapperSpan: 11 }
      },
      { 
        name: 'CX', required: false, type: 'button-group', 
        data: data, grid: 12/ 24 
      },
    ];
    return (
      <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
        <div>
          <div className={styles.create}>
            <EditFrom ref="findForm" daoName={daoName} controls={controlsFilter} clearItemMargin={true} record={{ qygm: '全部' }} selectFirst={false} />
          </div>
          <CompClieQylxWhForm ref="divi" jlid={record.ID} title={"【" + record.KHMC + "】 企业类型维护"} showEdit={showEdit} parentModel={daoName} />
          <DataTable ref="userTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
        </div>
      </div>
    )
  }
}
function mapStateToProps(state) {
  const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
  const { userID, orgID, pOrgID, popedomTypes } = state.menus;
  return {
    showEdit,
    selectedRowKeys,
    keyName,
    popedomTypes,
    record
  }
}
CM_CORP_CUST_DIM = connect(mapStateToProps)(CM_CORP_CUST_DIM)
export default CM_CORP_CUST_DIM;
let render = (props) => React.createElement(CM_CORP_CUST_DIM, props);
let f = new Factory()
f.set("CM_CORP_CUST_DIM", render)